// app.js
import {
  Toast
} from './libs/util'
import {
  API_DOMAIN,
  setActivityList,
  staticAdress
} from './utils/api.config'
const wechat = require('./utils/wechat.js')
const api = require('./utils/api.js')
const com = require('./utils/conmon.js')

App({

  /**
   * WeChat API
   */
  wechat: wechat,

  /**
   *  API
   */
  api: api,
  Toast: Toast,
  isLogin: false,
  userInfo: {},
  ifTeacher: false,
  loginOut() {

    this.isLogin = false

    wx.setStorageSync('tokenId', '');
    wx.setStorageSync('userData', '')
    wx.setStorageSync('ifTeacher', 'false')
    wx.setStorageSync('stuIndex', 0)
    this.userInfo = {};

    api.loginOut().then(rest => {
        if (rest && rest.success) {
          console.log('退出登录成功')
        } else {
          console.log('退出登录失败=' + rest.errMsg)
        }
      })
      .catch(r => {
        console.error('退出登录异常=' + r)
      })
  },
  upDataUserInfo(callback, isNotGoMine) {
    console.log('接口数据b2=' + isNotGoMine)
    api.getUserInfo({}).then(rest => {

        this.userInfo = rest.data
        console.log('接口数据b2=' + JSON.stringify(rest.data))

        wx.setStorageSync('userData', this.userInfo)
        if (rest && rest.success) {

          if (isNotGoMine) {
            if (callback) {
              callback(this.userInfo)
            }
          } else {

            this.userInfo = rest.data
            console.log('接口数据b2=' + JSON.stringify(rest.data))

            wx.setStorageSync('userData', this.userInfo)
            if (!isNotGoMine) { //不需要跳转mine
              if (rest.data.userType == 1 || this.userInfo.userType == 3) { //1老师 2家长 3 老师/家长
                this.ifTeacher = true
                wx.setStorageSync('ifTeacher', true)
              } else {
                wx.setStorageSync('ifTeacher', false)
                this.ifTeacher = false

              }
              wx.showToast({
                title: '登录成功',
                icon: "success",
                success: function () {
                  wx.switchTab({
                    url: '/pages/mine/mine'
                  })
                },
              })
            }
            if (callback) {
              callback(this.userInfo)
            }

          }
        }
      })
      .catch(r => {
        console.error(r)


      })
  },



  globalData: {
    userInfo: null,
    toTime: function (value) {
      value = parseInt(value)
      console.log(value)
      return (Math.floor(value / 60) + ':' + Math.floor(value % 60 + ':'))
    }
  },
  getActivList() {


  },

  onLaunch() {
    console.log("当前 onLaunch");
    wx.setStorageSync('staticAdress', staticAdress);

    var tokenId = wx.getStorageSync('tokenId');
    if (tokenId) {
      var user = wx.getStorageSync('userData')

      if (user) {
        this.userInfo = user
        this.ifTeacher = wx.getStorageSync('ifTeacher')
      }
    } else {
      this.loginOut()
    }



    this.getActivList();
    wx.onNetworkStatusChange((res) => {
      let msg = ''
      if (!res.isConnected) {
        msg = '当前网络不可用，请检查你的网络设置'
      } else if (res.networkType === 'none') {
        msg = '网络开小差了,请网络良好后重试'
      }
      if (msg) {
        wx.showToast({
          title: msg,
          icon: 'none',
        })
      }
    })


    // wx.setStorageSync('tokenId','')
    // wx.setStorageSync('userData', '');
    // 展示本地存储能力
    // const logs = wx.getStorageSync('logs') || []
    // logs.unshift(Date.now())
    //wx.setStorageSync('logs', logs)


    var that = this;

    var tokenId = wx.getStorageSync('tokenId');
    if (tokenId) {
      wx.login({
        success: function (res) {

          var code = res.code; //发送给服务器的code

          var msg = res.userInfo || {}
          msg.wxCode = code;
          wx.setStorageSync('userInfo', msg);

          wx.getUserInfo({
            success: function (res) {
              var userNick = res.userInfo.nickName; //用户昵称
              var avataUrl = res.userInfo.avatarUrl; //用户头像地址
              var gender = res.userInfo.gender; //用户性别
              if (code) {

                console.log("微信code1" + code)

                var msg = res.userInfo || {}
                msg.wxCode = code;

                wx.setStorageSync('userInfo', msg);
              } else {
                console.log("获取用户登录态失败！");
              }
            }
          })
        },
        fail: function (error) {
          console.log('login failed ' + error);
        }
      })
    }
  },



  globalData: {
    userInfo: null,
    version: 'V1.0.0'
  }
})